package com.xxcxcxcx.xc_moremob.common.skills.SkillSummon;

import com.xxcxcxcx.xc_moremob.common.entities.monster.realization.FlyMeleeBasicEntity;
import com.xxcxcxcx.xc_moremob.common.entities.monster.realization.SkillBasicEntity;
import com.xxcxcxcx.xc_moremob.common.registers.EntityRegister;
import com.xxcxcxcx.xc_moremob.common.skills.Currentskills;
import com.xxcxcxcx.xc_moremob.common.skills.UseskillGoal;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.SpawnGroupData;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;

//召唤生物
public class SummonFlyMeleeSkillGoal extends UseskillGoal {
    private final TargetingConditions vexCountTargeting = TargetingConditions.forNonCombat().range(16.0D).ignoreLineOfSight().ignoreInvisibilityTesting();

    public SummonFlyMeleeSkillGoal(SkillBasicEntity mo) {
        super(mo,20,100,340);
    }
    public SummonFlyMeleeSkillGoal(SkillBasicEntity mo, int CastWarmup, int Casting, int CastingInterval){
        super(mo,CastWarmup,Casting,CastingInterval);
    }
    public boolean canUse() {
        if (!super.canUse()) {
            return false;
        } else {
            int i = mob.level.getNearbyEntities(FlyMeleeBasicEntity.class, this.vexCountTargeting, mob, mob.getBoundingBox().inflate(16.0D)).size();
            return mob.level.getRandom().nextInt(8) + 1 > i;
        }
    }
    protected void performSpellCasting() {
        ServerLevel serverlevel = (ServerLevel)mob.level;
        for(int i = 0; i < 3; ++i) {
            BlockPos blockpos = mob.blockPosition().offset(-2 + mob.level.getRandom().nextInt(5), 1, -2 + mob.level.getRandom().nextInt(5));
            FlyMeleeBasicEntity SummonMob  = EntityRegister.FLY_MELEE_BASIC.get().create(mob.level);
            SummonMob.moveTo(blockpos, 0.0F, 0.0F);
            SummonMob.finalizeSpawn(serverlevel, mob.level.getCurrentDifficultyAt(blockpos), MobSpawnType.MOB_SUMMONED, (SpawnGroupData)null, (CompoundTag)null);
            SummonMob.setOwner(mob);
            SummonMob.setBoundOrigin(blockpos);
            SummonMob.setLimitedLife(20 * (30 + mob.level.getRandom().nextInt(90)));
            serverlevel.addFreshEntityWithPassengers(SummonMob);
        }
    }
    protected SoundEvent getSpellPrepareSound() {
        return SoundEvents.EVOKER_PREPARE_SUMMON;
    }
    protected Currentskills getSpell() {
        return Currentskills.SUMMON_VEX;
    }
}